using System;
using System.Collections.Generic;
using RockHopper.Common.Data;
using Windows.ApplicationModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;

namespace RockHopper.TrainingCalculator.WinRT.Controls
{
    public class VdotPopupControl : Control
    {
        private VdotControl sliderPart;
        private RacePerformanceControl racePerformancePart; 
        private readonly Dictionary<FrameworkElement, double> panels = new Dictionary<FrameworkElement, double>();
        private static readonly ISettingsRepository settingsRepository;

        public VdotPopupControl()
        {
            DefaultStyleKey = typeof (VdotPopupControl);
        }

        static VdotPopupControl()
        {
            if (!DesignMode.DesignModeEnabled)
            {
                settingsRepository = Global.Container.Resolve<ISettingsRepository>();
            }   
        }
       
        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            sliderPart = (VdotControl)GetTemplateChild("VdotSliderPart");
            panels.Add(sliderPart, 100);

            var vdotOption = (RadioButton) GetTemplateChild("VdotModeToggleButtonPart");
            vdotOption.Checked += OnVdotOptionChecked;
           
            var racePerfOption = (RadioButton)GetTemplateChild("RaceTimeModeToggleButtonPart");
            racePerfOption.Checked += OnRacePerfChecked;

            racePerformancePart = (RacePerformanceControl)GetTemplateChild("RacePerformanceSliderPart");
            panels.Add(racePerformancePart, 300);

            vdotOption.IsChecked = true;
        }

        private void OnRacePerfChecked(object sender, RoutedEventArgs args)
        {
            SlideOut(sliderPart, (s, e) => SlideIn(racePerformancePart));
        }

        private void OnVdotOptionChecked(object sender, RoutedEventArgs args)
        {
            SlideOut(racePerformancePart, (s, e) => SlideIn(sliderPart));
        }

        private void SlideIn(FrameworkElement instance)
        {
            var storyboard = new Storyboard();
            var duration = new Duration(TimeSpan.FromMilliseconds(250));
            var animation = new DoubleAnimation
            {
                From = 0,
                To = panels[instance],
                Duration = duration,
                EnableDependentAnimation = true,
                EasingFunction = new PowerEase { EasingMode = EasingMode.EaseOut }
            };
            Storyboard.SetTarget(animation, instance);
            Storyboard.SetTargetProperty(animation, "Height");
            storyboard.Children.Add(animation);
            storyboard.Begin();
        }

        private void SlideOut(FrameworkElement instance, EventHandler<object> finishedHandler)
        {
            var storyboard = new Storyboard();
            var duration = new Duration(TimeSpan.FromMilliseconds(250));
            var animation = new DoubleAnimation
            {
                From = panels[instance],
                To = 0,
                Duration = duration,
                EnableDependentAnimation = true,
                EasingFunction = new PowerEase { EasingMode = EasingMode.EaseOut }
            };
            Storyboard.SetTarget(animation, instance);
            Storyboard.SetTargetProperty(animation, "Height");
            storyboard.Children.Add(animation);
            storyboard.Completed += finishedHandler;
            storyboard.Begin();
        }
    }
}